﻿<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";
  
header('Content-Type: text/plain');

$q = json_decode(MiscUtils::getParam('q', NULL));


try {
  $myManager->start_t();
  foreach ($q as &$tmp) {
    $orm = classToOrm($tmp->n);
    if ($orm) {
      foreach ($tmp->a as &$obj) {
        if (isset($obj->e_oid) || isset($obj->id)) {
          $orm->update($myManager, $obj);
        } else {
          $orm->add($myManager, $obj);
        }
      }
      foreach ($tmp->r as &$obj) {
        $orm1 = classToOrm('produceevent');
        $orm2 = classToOrm('producetrack');
        $orm21 = classToOrm('producetrackuser');
        $orm3 = classToOrm('producepurchaseitem');
        $orm31 = classToOrm('producepurchaseitemselect');
        $orm4 = classToOrm('producenote');
        $orm5 = classToOrm('produceuser');
        $orm6 = classToOrm('produceselect');
        if (isset($obj->e_oid) || isset($obj->id)) {
          $result1 = $orm1->find($myManager, NULL, NULL, NULL, NULL, "d.produce_trackId='$obj->trackId'", NULL);
          foreach($result1->data as &$event) {
            $orm1->remove($myManager, $event);
          }
          $result2 = $orm2->find($myManager, NULL, NULL, NULL, NULL, "d.produce_id=$obj->id", NULL);
          foreach($result2->data as &$track) {
            $result21 = $orm21->find($myManager, NULL, NULL, NULL, NULL, "d.track_trackId='$track->trackId'", NULL);
            foreach($result21->data as &$tu) {
              $orm21->remove($myManager, $tu);
            }
            $orm2->remove($myManager, $track);
          }
          $result3 = $orm3->find($myManager, NULL, NULL, NULL, NULL, "d.produce_trackId='$obj->trackId'", NULL);
          foreach($result3->data as &$pi) {
            $result31 = $orm31->find($myManager, NULL, NULL, NULL, NULL, "d.purchase_trackId='$pi->trackId'", NULL);
            foreach($result31->data as &$sel) {
              $orm31->remove($myManager, $sel);
            }
            $orm3->remove($myManager, $pi);
          }
          $result4 = $orm4->find($myManager, NULL, NULL, NULL, NULL, "d.produce_trackId='$obj->trackId'", NULL);
          foreach($result4->data as &$note) {
            $orm4->remove($myManager, $note);
          }
          $result5 = $orm5->find($myManager, NULL, NULL, NULL, NULL, "d.produce_id=$obj->id", NULL);
          foreach($result5->data as &$user) {
            $orm5->remove($myManager, $user);
          }
          $result6 = $orm6->find($myManager, NULL, NULL, NULL, NULL, "d.produce_trackId='$obj->trackId'", NULL);
          foreach($result6->data as &$select) {
            $orm6->remove($myManager, $select);
          }
        }
        $orm->remove($myManager, $obj);
      }
    }
  }
  $myManager->commit_t();
  
  echo json_encode(array());
} catch (PDOException $e) {
  echo MiscUtils::encode($e->getMessage());
}
?>